<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <span id="show"></span>
    <script>
        // var date1 = new Date('2020-6-19 20:00:00')
        // var date2 = new Date('2020-6-19 22:00:00')
        // console.log(chatime(date1, date2))
        function chatime(date1, date2) {
            var time1 = date1.getTime();
            var time2 = date2.getTime();
            var res = Math.abs((time1 - time2));
            var day = parseInt((res / 1000) / 60 / 60 / 24);
            var h = parseInt((res / 1000) / 60 / 60 % 24);
            var m = parseInt((res / 1000) / 60 % 60)
            var s = parseInt((res / 1000) % 60);
            return '在页面停留' + day + '天' + h + '小时' + m + '分' + s + '秒'
        }
        var preDate = new Date();
        var newDate = new Date();
        var res = chatime(preDate, newDate)
        show.innerHTML = res;

       
        setInterval(function () {
            newDate = new Date();
            var res = chatime(preDate, newDate)
            show.innerHTML = res;
        }, 1000)
    </script>
</body>

</html>